﻿(function () {
    "use strict";

    // Get a reference for an item, using the group key and item title as a
    // unique reference to the item that can be easily serialized.
    function getItemReference(item) {
        return [item.group.key, item.title];
    }

    function resolveGroupReference(key) {
        for (var i = 0; i < groupedItems.groups.length; i++) {
            if (groupedItems.groups.getAt(i).key === key) {
                return groupedItems.groups.getAt(i);
            }
        }
    }

    function resolveItemReference(reference) {        
        for (var i = 0; i < groupedItems.length; i++) {
            var item = groupedItems.getAt(i);
            if (item.group.key === reference[0] && item.title === reference[1]) {
                return item;
            }
        }
    }

    function getItemFromKey(key) {
        for (var i = 0; i < groupedItems.length; ++i) {
            if (groupedItems.getAt(i).key === key) {
                return groupedItems.getAt(i);
            }
        }
    }

    // This function returns a WinJS.Binding.List containing only the items
    // that belong to the provided group.
    function getItemsFromGroup(group) {
        return list.createFiltered(function (item) { return item.group.key === group.key; });
    }

    // Init list.
    var list = new WinJS.Binding.List();

    //WinJS.xhr({ url: "data/products.txt" }).then(function (xhr) {
    //    var items = JSON.parse(xhr.responseText);

    //    // Add the items to the WinJS.Binding.List
    //    items.forEach(function (item) {
    //        list.push(item);
    //    });
    //});

    // Get data from server.
    WinJS.xhr({ url: HOST_NAME + "/defaultPromotion/?lang=en" }).then(function (xhr) {
        if (xhr.status == 200) {
            // Parse xhr to JSON.
            var response = JSON.parse(xhr.response);

            // Get category size.
            var categorySize = response.data.length;
            for (var i = 0; i < categorySize; ++i) {
                // Get category item.
                var category = response.data[i];

                // Get useful info from JSON object.
                var categoryId = category.categoryId;
                var categoryName = category.categoryName;
                var categoryDesc = category.categoryDesc;
                var categoryOrder = category.catOrder;
                var groupBackgroundImage = category.catImages.detail;
                var groupImage = category.catImages.group;

                // Get the number of merchants.
                var merchantSize = category.merchants.length;
                for (var j = 0; j < merchantSize; ++j) {
                    // Get merchant item.
                    var merchant = category.merchants[j];

                    var merchantOrder = merchant.mOrder;
                    var merchantId = merchant.merchantId;
                    var merchantName = merchant.merchantName;
                    var merchantBackgroundImage = merchant.images.l;
                    var merchantTitleImage = merchant.images.s;
                    var merchantAddress = merchant.address;
                    var merchantDescription = merchant.desc;

                    var merchantObj = {
                        "backgroundImage": merchantBackgroundImage,
                        "group": {
                            "backgroundImage": groupBackgroundImage,
                            "description": categoryDesc != undefined ? categoryDesc : "",
                            "groupImage": groupImage,
                            "key": categoryName,
                            "shortTitle": categoryName,
                            "title": categoryName,
                            "button": categoryName
                        },
                        "key": merchantOrder,
                        "id": merchantId,
                        "shortTitle": merchantName,
                        "titleImage": merchantTitleImage,
                        "title": merchantName,
                        //"discount": "Discount",
                        "description": merchantDescription,
                        "address": merchantAddress,
                        "button": "Redeem Now",                        
                    };

                    //console.log(merchantObj.id);
                    //console.log(merchantObj.backgroundImage);
                    //console.log(merchantObj.key);
                    //console.log(merchantObj.shortTitle);                
                    //console.log(merchantObj.titleImage);
                    //console.log(merchantObj.title);
                    //console.log(merchantObj.discount);
                    //console.log(merchantObj.address);
                    //console.log(merchantObj.button);

                    list.push(merchantObj);
                }
            }
        } else {
            $("#error").text("SOMETHING WRONG");
        }

        return xhr;
    }).done(
            function onComplete(result) {
                //console.log(result.responseText);
            },

            function onError(error) {
                console.log(error.message);
            }
    );

    var groupedItems = list.createGrouped(
        function groupKeySelector(item) { return item.group.key; },
        function groupDataSelector(item) { return item.group; }

        // Hieu: Compare groups.
        // TODO: Need improve it to check and compare the groupKey.
        , function compareGroup(left, right) { return -1; }
    );

    WinJS.Namespace.define("Data", {        
        items: groupedItems,
        groups: groupedItems.groups,
        getItemsFromGroup: getItemsFromGroup,
        getItemReference: getItemReference,
        resolveGroupReference: resolveGroupReference,
        resolveItemReference: resolveItemReference,
        getItemFromKey: getItemFromKey
    });
})();
